package com.ht.util;

import com.ht.config.DBConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBHelper {

    private static Connection connection = null;

    static {
        try {
            Class.forName(DBConfig.DRIVER);
            connection = DriverManager.getConnection(DBConfig.URL, DBConfig.USERNAME, DBConfig.PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * @return Connection
     */
    public static Connection getConnection() {
        if (connection == null) {
            try {
                Class.forName(DBConfig.DRIVER);
                connection = DriverManager.getConnection(DBConfig.URL, DBConfig.USERNAME, DBConfig.PASSWORD);
                return connection;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

}